package com.zztl.infiniteBand.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter

import com.zztl.infiniteBand.adapter.LanguageAdapter
import com.zztl.infiniteBand.base.BaseActivity
import com.zztl.infiniteBand.model.LanguageBean
import kotlinx.android.synthetic.main.activity_setting_language.*

import android.app.Activity
import android.content.Intent
import android.location.LocationManager
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.util.language.LanguageManager


class SettingLanguageActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {


    lateinit var mAdapter:LanguageAdapter
    val languageList= mutableListOf<LanguageBean>()
    override fun setLayoutId(): Int = com.zztl.infiniteBand.R.layout.activity_setting_language

    override fun initBase() {
        setTitle(getString(R.string.语言))
        val language = LanguageManager.getSelectLanguage(this)
        when(language){
            getString(com.zztl.infiniteBand.R.string.英语)->{
                languageList.add(LanguageBean(language,true))
                languageList.add(LanguageBean(getString(R.string.简体中文),false))
            }
            getString(com.zztl.infiniteBand.R.string.简体中文)->{
                languageList.add(LanguageBean(getString(R.string.英语),false))
                languageList.add(LanguageBean(language,true))
            }

        }
        mAdapter= LanguageAdapter(com.zztl.infiniteBand.R.layout.item_language,languageList).apply {
            onItemClickListener = this@SettingLanguageActivity
        }
        rectclerview.run {
            adapter=mAdapter

        }
    }
    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
       languageList.get(position).run {
           if(!isChoosed){
               languageList.forEach {
                   it.isChoosed=false
               }
               isChoosed=true
               mAdapter.notifyDataSetChanged()
               selectLanguage(position)
           }
       }
    }

    private fun selectLanguage(select: Int) {
        LanguageManager.saveSelectLanguage(this, select)
//        MainActivity.reStart(this)
//        finish()
        Intent(this,SettingActivity::class.java).apply {
            setResult(Activity.RESULT_OK)
            skipActivity(this)
        }
    }
}
